package com.mediocre.customLogin.controller;

import com.mediocre.customLogin.util.VerifyCodeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @desc
 * @author 张伟
 * @date 2022/1/2 下午2:07
 * @modifier
 */
@RestController
public class LoginController {
    @GetMapping("/getVerifyCode")
    public void code(HttpSession session, HttpServletResponse resp) throws IOException {
        VerifyCodeUtil verifyCodeUtil = new VerifyCodeUtil();
        BufferedImage image = verifyCodeUtil.getImage();
        String text = verifyCodeUtil.getText();
        session.setAttribute("verify_code", text);
        // 设置响应的内容类型为 image/jpeg，注意：这里必须设置，否则可能返回到前端页面上的是乱码
        resp.setContentType("image/jpeg");
        VerifyCodeUtil.output(image, resp.getOutputStream());
    }
}
